<?php

namespace App\Http\Controllers\Api\v1;

use App\common;
use App\Http\Controllers\Controller;
use App\Logic\BorkerLogic;
use App\Logic\BrokerLogic;
use App\Logic\HouseLogic;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class BrokerController extends Controller
{

    /**
     * @name  wl
     * @function 展示方法
     * @param Request $request
     * @return array|false|string
     */
    public function brokerShow(Request $request)
    {
        if ($request->method() != 'GET') {
            return common::fail('', '', '请求方式不正确');
        } else {
            try {
                $showLogic = BrokerLogic::show();
                return common::success(20000, $showLogic, "数据已查到");
            } catch (\Exception $exception) {
                return common::fail('', "服务异常", $exception->getMessage());
            }
        }
    }

    /**
     * @function 删除方法
     * @param Request $request
     * @return array|false|string
     */
    public function brokerDel(Request $request)
    {
        if ($request->method() != 'GET')  //当前请求是否为 GET 请求
        {
            return common::fail('422', 'null', '请求方式失败');
        } else {
            try {
                $id = $request->get('id');
                $delLogic = BrokerLogic::del($id);
                if ($delLogic) return common::success(20000, $delLogic, '删除成功');
            } catch (\Exception $exception) {
                return common::fail('', "服务异常", $exception->getMessage());
            }
        }
    }

    /**
     * @function 搜索方法
     * @param Request $request
     * @return array|false|string
     */
    public function brokerSearch(Request $request)
    {
        if ($request->method() == 'POST')  //当前请求是否为 GET 请求
        {
            try {
                $message = $request->post('search');
                $results = BrokerLogic::searchFunction($message);
                return common::success(20000, $results, '数据已查到');
            } catch (\Exception $exception) {
                return common::fail('', "", $exception->getMessage());
            }
        } else {
            return common::fail('', '', '请求方式不正确');
        }
    }

    /**
     * @function  修改方法
     * @param Request $request
     * @return array|false|string
     */
    public function brokerUpdate(Request $request)
    {
        try {
            $this->validate($request, [
                'name' => 'required|unique:zfm_brokers',
                'phone' => 'required',
                'firm' => 'required'
            ]);
            $BrokerData = $request->post();
            $updateLogic = BorkerLogic::BorkerSave($BrokerData);
            if (!$updateLogic) return common::success(20000, $updateLogic, '修改成功');
        } catch (ValidationException $validationException) {
            return common::fail('', "", $validationException->errors());
        }
    }
}
